package com.zhht.htzx.ui;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.bean.UserDataBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.ToastUtils;
import com.nsgf.library.widget.prckerview.OptionsPickerView;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityUserRecordSettingBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理
 */
public class UserRecordSettingActivity extends BaseActivity {

    private UserRecordStateHolder mStates;

    private AccountRequester mAccountRequester;


    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(UserRecordStateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {

        return new DataBindingConfig(R.layout.activity_user_record_setting, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    private ActivityUserRecordSettingBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mStates.userDetail.set(getIntent().getParcelableExtra("data"));
        mStates.rankName.set(mStates.userDetail.get().getRankName());
        mStates.spId.set(mStates.userDetail.get().getSpid());
        mBinding = (ActivityUserRecordSettingBinding) getBinding();
        mStates.titleBar.set("修改信息");
        mAccountRequester.getRanks();
        mAccountRequester.getLibraryList().observe(this, new Observer<ResponseStatus<List<LibraryInfo>>>() {
            @Override
            public void onChanged(ResponseStatus<List<LibraryInfo>> listResponseStatus) {
                if (listResponseStatus.getResponseCode() == 0) {
                    list.addAll(listResponseStatus.getData());
                }
            }
        });
        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {
                if (stringResponseStatus.getResponseCode() == 0) {
                    ToastUtils.showShortToast(stringResponseStatus.getMsg());
                    finish();
                }
            }
        });
    }

    private List<LibraryInfo> list = new ArrayList<>();

    private String rank;

    public class ClickProxy {
        public void submitUserInfo() {
            mAccountRequester.updateLevelAndStar(String.valueOf(mStates.userDetail.get().getIdX()), rank, mStates.spId.get());
        }

        public void rankList() {


            OptionsPickerView<LibraryInfo> mOptionsPickerView = new OptionsPickerView.Builder(UserRecordSettingActivity.this
                    , new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {

                    mStates.rankName.set(list.get(options1).getPickerViewText());
                    rank = list.get(options1).getId();
                }
            }).setTitleText("请选择分类").build();
            mOptionsPickerView.setPicker(list);
            mOptionsPickerView.show();

        }
    }

    public static class UserRecordStateHolder extends StateHolder {

        public final State<UserDataBean.ListBean> userDetail = new State<>(new UserDataBean.ListBean());
        public final State<String> rankName = new State<>("");
        public final State<String> spId = new State<>("");
    }
}
